#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

log_level=${log_level:=info}
assist_dir=${assist_dir:=$this_script_dir/../assist}
source $assist_dir/global.sh

################################################################################
#                                                                          80col
print_help()
{
    cat <<EOF
    useage:
         $0 ip
         $0 ip --log_level=debug

         $0 --ipu --host_id=home_office
         $0 --ip_upload --loop                #looply with default 60 seconds interval time
         $0 --ip_upload --loop --interval=5   #loopy with 5 seconds interval

    --ip              - print out public ip and loacl ip of the host

    --ver             - print current os version info
    --version         - print current os version info , same as ver

    --ip_upload       - upload current public ip to remote http server
    --host_id=string  - upload ip with user setting hostid ,default is $HOSTNAME
    --loop            - upload ip looply
    --interval=num    - upload ip looply with $interval second, default num=60

    --log_level=<debug>   - control pint log on streen,trace|debug|info|warn|error|critical

    --help  - prints help screen, script will exit after printing


EOF
}

get_public_ip()
{
    init_var
    vlogd "the cmd that get public ip is:$cmd_str"
    vlogi "public ip is:$($cmd_str)"
}

get_local_ip()
{
    local ip_strs
    local ips
    ip_strs=$(ifconfig | grep inet | grep broadcast | awk -F ' ' '{printf "%s ", $2}' )
    #echo ${#ip_strs[*]}
    ips=($ip_strs)
    #echo ${ips[*]}
    #echo ${ips[@]}
    vlogi " local  ip is:${ips[*]}"
}

init_var()
{
    ip_url=http://bc.igcc.cc:7300/tool/ip
    #curl -s -w '\n' -X GET  $ip_url   #end with \n
    cmd_str="curl -s $ip_url"
}


install_iostat()
{
    if ! type iostat > /dev/null 2>&1 ; then
        if uname -a | grep -i ubuntu ; then
            vlogi "you have to install iostat firstly ..."
            sudo apt update
            sudo apt install -y sysstat
        fi
    fi
}

main()
{
    if [ -n "$help" ] || [ $_arg_num_ -eq 0 ]  ; then
        print_help

    elif [ -n "$io" ] ; then
        install_iostat
        local log_dir=$HOME/run/log/monitor_board
        [ ! -d $log_dir ] && mkdir -p $log_dir
        local log_file_tmp=$log_dir/iostat_$(date "+%Y%m%d").log
        vlogi "iostat succeed, check log: tail -f \"$log_file_tmp\""
        if [ "$io" == "io" ] ; then
            device="/dev/sdc /dev/sda /dev/sdd"
        else
            device="$io"
        fi
        iostat -cdtyhx  -t 5 99999 $device | tee -a "$log_file_tmp"

    elif [ -n "$version" ] || [ -n "$ver" ] ; then
        $this_script_dir/linux_version.sh

    elif [ -n "$ip_upload" ] || [ -n "$ipu" ]  ; then
        #check_argument host_id || exit 1
        if [ -z "$host_id" ] ; then
            host_id=$HOSTNAME
        fi

        init_var
        public_ip=$($cmd_str)
        cmd_ipu=$(cat <<EOF
        curl -s -X POST -d '{"id":"$host_id","ip":"$public_ip"}'  "http://bc.igcc.cc:7300/tool/ips"
EOF
)
        cmd_ipu=$(echo -n $cmd_ipu)
        vlogi "$cmd_ipu"
        eval "$cmd_ipu"

        if [ -n "$loop" ] ; then
            [ -z "$interval" ] && local interval=60
            while [ true ] 
            do
                $this_script_dir/show.sh --ip_upload
                sleep $interval
            done
        fi
    else
        vloge "parameter is not found."
        print_help

    fi

    return 0
}

main || exit 1 && exit 0

